package com.ssy.lingxi.member.merchant.serviceimpl.web;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.config.ServiceConfig;
import com.ssy.lingxi.member.merchant.entity.MemberRelationDO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.response.MemberDetailRightHistoryVO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.response.MemberDetailRightSpendHistoryVO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.response.MemberDetailRightVO;
import com.ssy.lingxi.member.merchant.model.vo.validate.request.MemberValidateVO;
import com.ssy.lingxi.member.merchant.model.vo.validate.request.ValidateIdPageVO;
import com.ssy.lingxi.member.merchant.repository.MemberRelationRepository;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberCacheService;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberDetailService;
import com.ssy.lingxi.member.merchant.service.web.IPlatformMemberDetailRightService;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 平台后台 - 会员维护 - 会员详情 - 权益信息服务接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-07-16
 */
@Service
public class PlatformMemberDetailRightServiceImpl implements IPlatformMemberDetailRightService {
    @Resource
    private IBaseMemberCacheService memberCacheService;

    @Resource
    private MemberRelationRepository relationRepository;

    @Resource
    private IBaseMemberDetailService baseMemberDetailService;

    /**
     * 查询会员详情 - 会员权益信息
     * @param headers HttpHeaders信息
     * @param validateVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<MemberDetailRightVO> getMemberDetailRight(HttpHeaders headers, MemberValidateVO validateVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberRelationDO relationDO = relationRepository.findById(validateVO.getValidateId()).orElse(null);
        if(relationDO == null || !relationDO.getMemberId().equals(loginUser.getMemberId()) || !relationDO.getSubMemberId().equals(validateVO.getMemberId())) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_RELATION_DOES_NOT_EXIST);
        }

        return baseMemberDetailService.getMemberDetailRight(relationDO);
    }

    /**
     * 查询会员详情 - 分页查询会员权益获取记录
     * @param headers HttpHeaders信息
     * @param pageVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<PageData<MemberDetailRightHistoryVO>> pageMemberDetailRightHistory(HttpHeaders headers, ValidateIdPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberRelationDO relationDO = relationRepository.findById(pageVO.getValidateId()).orElse(null);
        if(relationDO == null || !relationDO.getMemberId().equals(loginUser.getMemberId()) || !relationDO.getRoleId().equals(loginUser.getMemberRoleId())) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_RELATION_DOES_NOT_EXIST);
        }

        return baseMemberDetailService.pageMemberDetailRightHistory(relationDO, pageVO.getCurrent(), pageVO.getPageSize(), ServiceConfig.DEFAULT_DATETIME_FORMATTER);
    }

    /**
     * 查询会员详情 - 分页查询会员权益使用记录
     * @param headers HttpHeaders信息
     * @param pageVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<PageData<MemberDetailRightSpendHistoryVO>> pageMemberDetailRightSpendHistory(HttpHeaders headers, ValidateIdPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberRelationDO relationDO = relationRepository.findById(pageVO.getValidateId()).orElse(null);
        if(relationDO == null || !relationDO.getMemberId().equals(loginUser.getMemberId()) || !relationDO.getRoleId().equals(loginUser.getMemberRoleId())) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_RELATION_DOES_NOT_EXIST);
        }

        return baseMemberDetailService.pageMemberDetailRightSpendHistory(relationDO, pageVO.getCurrent(), pageVO.getPageSize(), ServiceConfig.DEFAULT_DATETIME_FORMATTER);
    }
}
